<form name="form1" method="post">
<table width="600" border="0">
  <tr>
    <td width="100">Beneficiario:</td>
		<td colspan="3" ><?=helpers::combo($conn, "", $objeto->id_beneficiario,"","","id_beneficiario","id","nombre","id_beneficiario","","SELECT * FROM proveedores AS A WHERE EXISTS (SELECT id FROM egresos AS B WHERE B.id_zona = " . $_SESSION['SesionZona'] . " AND B.saldo>0 AND B.id_proveedor = A.id LIMIT 1) ORDER BY A.nombre","CargarDeudas();","1")?></td>		
   </td>
  </tr>

<tr id="Fecha">
	<td>Fecha:</td>
	<td >
		<table>
			<tr>
				<td><input value="<?=empty($objeto->fecha) ? date("d/m/Y") : muestrafecha($objeto->fecha)?>" name="fecha" id="fecha"  type="text" style="width:80px" /></td> 
				<td > 
					<a href="#" id="boton_fecha" onclick="return false;"><img border="0" src="images/calendarA.png" width="20" height="20" /></a>  
					<script type="text/javascript">
						new Zapatec.Calendar.setup({
								firstDay          : 1,
								weekNumbers       : true,
								showOthers        : false,
								showsTime         : false,
								timeFormat        : "24",
								step              : 2,
								range             : [1900.01, 2999.12],
								electric          : false,
								singleClick       : true,
								inputField        : "fecha",
								button            : "boton_fecha",
								ifFormat          : "%d/%m/%Y",
								daFormat          : "%Y/%m/%d",
								align             : "Br"
							});
					</script>
				</td>
			</tr>
		</table>
	</td>
</tr>

<tr>
	<td>Observaciones:</td>
	<td colspan="2">
		<textarea name="observaciones" id="observaciones" rows="3" cols="44" ><?=$objeto->observaciones?></textarea>
	</td>
</tr>

	<tr>
		<td colspan="2" align="center"><hr /><span onClick="MostrarII()" style="cursor:pointer" class="titulo">Cuentas Por Pagar</span></td>
	</tr>

	<tr id="TablaII" >
		<td colspan="2">
			<table border="0" >					
				<tr>
					<td colspan="2">
						<div id="gridbox2" width="600" height="150" class="gridbox"></div>
					</td>
				</tr>
				<tr>
					<td width="100px">Total Seleccionado:</td>
					<td><input type="text" name="total_sel" readOnly="true" id="total_sel" value="0,00">
					</td>
				</tr>


			</table>
		</td>
	</tr>

	<tr>
		<td colspan="2" align="center"><hr /><span onClick="MostrarI()" style="cursor:pointer" class="titulo">Formas De Pago</span></td>
	</tr>

	<tr id="TablaI" >
		<td colspan="2">
			<table border="0" >				
				<tr>
					<td>Forma:</td>
					<td >
						<SELECT NAME="forma_pag" id="forma_pag" onChange="bancos_visible()" >
							<OPTION value="-1">Seleccione
							<OPTION value="3">Cheque
							<OPTION value="4">Efectivo
							<OPTION value="5">Transferencia

						</SELECT>
					</td>
				</tr>

				<tr id="Bancos" style="display:none">
					<td >Cuenta:</td>
					<td colspan="2" ><?=helpers::combo($conn, "", "","","","banco_pag","id","cuenta","banco_pag","","SELECT A.id, B.nombre || ' - ' || A.numero AS cuenta FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id ORDER BY B.nombre, A.numero","BuscaCheque();","1")?></td>
				</tr>
				
				<tr id="Cheque" style="display:none">
					<td>Cheque:</td>
					<td colspan="2">
					<input type="text" name="cheque_pag" id="cheque_pag" style="width:100px" onChange="VerificaCheque();">
					</td>
				</tr>
				
				<tr id="Referencia" style="display:none">
					<td>Referencia:</td>
					<td colspan="2">
					<input type="text" name="ref_pag" id="ref_pag" style="width:250px">
					</td>
				</tr>

				<tr>
					<td width="100px">Monto:</td>
					<td colspan="2" ><input type="text" name="monto_pag" id="monto_pag" onClick="click_monto();" onBlur="monto_defecto();" value="0,00">
					</td>
				</tr>
	
				<tr>
					<td align="left" colspan="2"  ><input type="button" value="Agregar" onClick="Agregar()"></td>
					<td align="right" colspan="2"  ><input type="button" value="Eliminar" onClick="Eliminar()"></td>
				</tr>
	
				<tr>
					<td colspan="2">
						<div id="gridbox" width="600" height="150" class="gridbox"></div>
					</td>
				</tr>

				<tr>
					<td width="100px">Total Pagado:</td>
					<td><input type="text" readOnly="true" name="total_pag" id="total_pag" value="0,00">
					</td>
				</tr>


			</table>
		</td>
	</tr>


</table>

<input style="float:right"  name="accion" type="button" value="<?=$boton?>" onclick="if(ValidaMontos()){Guardar(); <?=$validator->validate() ?>;} else {alert('Error: El Monto De Cuentas Por Pagar Seleccionadas Debe Ser Igual Al Monto De Las Formas De Pago');}" />
<input name="id" type="hidden" value="<?=$objeto->id?>" />
<input name="deudas" id="deudas" type="hidden" value="" />
<input name="pagos" id="pagos" type="hidden" value="" />
<input name="accion" type="hidden" value="<?=$boton?>" /> 
<span style="position:absolute;top:4px;right:5px;cursor:pointer;">
<img onclick="close_div();" src="images/close_div.gif" /></span>
</form>
<p class="errormsg">(*) Campo Requerido</p

<script type="text/javascript">
buildGrid();
CargarGrid();
buildGrid2();
CargarGrid2();

function buildGrid(){
	//set grid parameters
	mygrid = new dhtmlXGridObject('gridbox');
	mygrid.selMultiRows = true;
	mygrid.setImagePath("js/grid/imgs/");
	mygrid.setHeader("Forma,Id,Cuenta,Referencia,Cheque,Monto");
	mygrid.setInitWidths("75,50,225,75,100,75");
	mygrid.setColAlign("left,left,left,left,left,right");
	mygrid.setColTypes("coro,ro,ro,ro,ro,ed");
	mygrid.setColSorting("int,int,str,int,str,int");
	mygrid.setColumnColor("white,white,white,white,white,white");
	mygrid.rowsBufferOutSize = 0;
	mygrid.setMultiLine(false);
	mygrid.selmultirows="true";
	mygrid.getCombo(0).put("3","Cheque");
	mygrid.getCombo(0).put("4","Efectivo");
	mygrid.getCombo(0).put("5","Transferencia");
	mygrid.delim=";";
	mygrid.attachEvent("onEditCell",TotalPago);
	//mygrid.setOnEditCellHandler(TotalPago);
	//start grid
	 mygrid.enableLightMouseNavigation(true); mygrid.init();
} 

function CargarGrid(){
var JsonAux;
	mygrid.clearSelection();
	mygrid.clearAll();
	if("<?=$objeto->id?>"){ 
		if('<?=$objeto->pagos?>'){
			var Pagos = eval( '(' + '<?=$objeto->pagos?>' + ')');
			for(i=0;i<Pagos.length;i++){
				if (Pagos[i][0]!=4){
					mygrid.addRow(i,Pagos[i][0]+";"+Pagos[i][1]+";"+Pagos[i][2]+";"+Pagos[i][3]+";"+Pagos[i][4]+";"+muestraFloat(parseFloat(Pagos[i][5]),2),i);
				} else {
					mygrid.addRow(i,Pagos[i][0]+";;;;;"+muestraFloat(parseFloat(Pagos[i][5]),2),i);
				}
			}
		}
	}
}

function buildGrid2(){
	//set grid parameters
	mygrid2 = new dhtmlXGridObject('gridbox2');
	mygrid2.selMultiRows = true;
	mygrid2.setImagePath("js/grid/imgs/");
	mygrid2.setHeader("Sel.,Id,Factura, Descripcion, Saldo, Monto");
	mygrid2.setInitWidths("50,50,100,250,75,75");
	mygrid2.setColAlign("center,left,left,left, right, right");
	mygrid2.setColTypes("ch,ro,ro,ro,ro,ed");
	mygrid2.setColSorting("int,int,int,str,int,int");
	mygrid2.setColumnColor("white,white,white,white,white,white,");
	mygrid2.rowsBufferOutSize = 0;
	mygrid2.setMultiLine(false);
	mygrid2.selmultirows="true";
	mygrid2.delim=";";
	mygrid2.attachEvent("onEditCell",TotalDeuda);
	//mygrid.setOnEditCellHandler(TotalDeuda);
	//start grid
	 mygrid2.enableLightMouseNavigation(true); mygrid2.init();
} 

function TotalDeuda(stage,rowId,cellInd, newValue, oldValue){
var Monto, sTotal, Total, tSaldo, fSaldo, Temp;
	Monto = 0;
	sTotal = 0;
	Total = 0;
	tSaldo = "0";
	fSaldo = "0";
	Temp = 0;
	if (stage='0') {
		if(cellInd = 6) {
			tSaldo = mygrid2.cells(rowId,4).getValue();
			fSaldo = tSaldo.replace(".","");
			fSaldo = fSaldo.replace(",",".");
			fSaldo = parseFloat(fSaldo);
			Temp = mygrid2.cells(rowId,5).getValue();
			Temp = Temp.replace(".","");
			Temp = Temp.replace(",",".");
			Temp = parseFloat(Temp);
			if (Temp > fSaldo || Temp <0) {
				mygrid2.cells(rowId,5).setValue(tSaldo);
				alert ("Error: El Monto A Cancelar Debe Ser Mayor A Cero Y Menor O Igual Al Saldo De La Cuenta Por Cobrar.");
			}
		}
	}


	if (stage='2') {
		for(i=0;i<mygrid2.getRowsNum();i++){
			if(mygrid2.cells(mygrid2.getRowId(i),0).isChecked()){
				Monto = mygrid2.cells(mygrid2.getRowId(i),5).getValue();
				Monto = Monto.replace(".","");
				Monto = Monto.replace(",",".");
				Total=parseFloat(Total) + parseFloat(Monto);
			}
		}
		Total = Total.toFixed(2);
		sTotal = Total.toString();
		sTotal = sTotal.replace(".",",");
		document.getElementById('total_sel').value = sTotal;
	}

	return true;
} 

function TotalPago(stage,rowId,cellInd, newValue, oldValue){
var Monto, sTotal, Total;
	Monto = 0;
	sTotal = 0;
	Total = 0;

	if (stage='2') {
		for(i=0;i<mygrid.getRowsNum();i++){
			Monto = mygrid.cells(mygrid.getRowId(i),5).getValue();
			Monto = Monto.replace(".","");
			Monto = Monto.replace(",",".");
			Total=parseFloat(Total) + parseFloat(Monto);
		}
		Total = Total.toFixed(2);
		sTotal = Total.toString();
		sTotal = sTotal.replace(".",",");
		document.getElementById('total_pag').value = sTotal;
	}
	return true;
} 


function CargarGrid2(){
var JsonAux;
	mygrid2.clearSelection();
	mygrid2.clearAll();
	JsonAux={"Beneficiario":parseInt($('id_beneficiario').options[$('id_beneficiario').selectedIndex].value)};
	var url = 'CargarDeudasCXP.php';
	var pars = 'JsonEnv=' + JsonAux.toJSONString();
	var Request = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
	//		asynchronous:true, 
			onComplete:function(request){
				var JsonRec = eval( '(' + request.responseText + ')');
				if(JsonRec){
					for(i=0;i<JsonRec.length;i++){
						mygrid2.addRow(i,"0;"+JsonRec[i]['ID']+";"+JsonRec[i]['F']+";"+JsonRec[i]['D']+";"+JsonRec[i]['M']+";"+JsonRec[i]['M'],i);
					}
				}
			}
		}
	); 
} 

</script>
